package com.taoyibao.hongcanting.ui.home.delegate;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.model.ShoppingCartModel;
import com.taoyibao.hongcanting.ui.home.views.CartListArea;
import com.taoyibao.hongcanting.utils.CodeUtils;

import java.util.List;

/**
 * @author TangAnna
 * @description: 提交订单
 * @date :${DATA} 16:09
 */
public class SubmitOrdersDelegate extends BackToolbarDelegate implements RadioGroup.OnCheckedChangeListener {
    /**
     * 下边的列表
     */
    private CartListArea mCartListArea;
    private RadioGroup mRadioGroup;
    /**
     * 是否要去扫码(默认不需要扫码)
     */
    public boolean isNeedQRCode = false;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_submit_orders;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("提交订单");
        setLoadingView();
        mCartListArea = get(R.id.scla_submitOrder_list);
        mCartListArea.isShowCountController(false);
        mRadioGroup = get(R.id.rg_submitOrder_way);
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    /**
     * 设置列表数据
     */
    public void setListData(List<ShoppingCartModel> shoppingCartModel) {
        if (shoppingCartModel == null || shoppingCartModel.size() < 1) {//没数据
            return;
        }

        mCartListArea.setData(shoppingCartModel);
        ((TextView) get(R.id.tv_submitOrder_price)).setText("总价:" + CodeUtils.formatAmountUnit(shoppingCartModel.get(0).discount_price));
        ((TextView) get(R.id.tv_submitOrder_discountPrice)).setText("已优惠:" + CodeUtils.formatAmountUnit((shoppingCartModel.get(0).origin_price - shoppingCartModel.get(0).discount_price)));
    }

    /**
     * 根据桌号更新页面UI
     */
    public void setSeatUI(OrderInfoModel orderInfoModel) {
        if (orderInfoModel != null) {//未支付订单中的桌位信息
            isNeedQRCode = false;
            get(R.id.layout_submit_order_seatNo).setVisibility(View.GONE);
            get(R.id.layout_submit_order_seatYes).setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(orderInfoModel.order_deskid)) {//未支付的订单是堂食
                //堂食
                ((TextView) get(R.id.tv_submit_order_seat)).setText(orderInfoModel.order_deskid);
                ((TextView) get(R.id.tv_submit_order_type)).setText(CodeUtils.getIdString(R.string.string_zhuoWei));
            } else {//未支付的订单时外带
                ((TextView) get(R.id.tv_submit_order_seat)).setText(CodeUtils.getIdString(R.string.string_waiDai));
                ((TextView) get(R.id.tv_submit_order_type)).setText(CodeUtils.getIdString(R.string.string_jiuCanWay));
            }
        } else {//读取本地的桌位信息
            //先判断是否是赠送或者本地有保存的桌位
            if (!TextUtils.isEmpty(SpUtils.getOtherSeatName()) || !TextUtils.isEmpty(SpUtils.getSeatName())) {//赠送 或者是已扫过码
                isNeedQRCode = false;
                if (!TextUtils.isEmpty(SpUtils.getSeatName())) {
                    get(R.id.layout_submit_order_seatNo).setVisibility(View.GONE);
                    get(R.id.layout_submit_order_seatYes).setVisibility(View.VISIBLE);
                    //堂食
                    ((TextView) get(R.id.tv_submit_order_seat)).setText(SpUtils.getSeatId());
                    ((TextView) get(R.id.tv_submit_order_type)).setText(CodeUtils.getIdString(R.string.string_zhuoWei));
                } else {
                    get(R.id.layout_submit_order_seatNo).setVisibility(View.GONE);
                    get(R.id.layout_submit_order_seatYes).setVisibility(View.VISIBLE);
                    ((TextView) get(R.id.tv_submit_order_seat)).setText(CodeUtils.getIdString(R.string.string_waiDai));
                    ((TextView) get(R.id.tv_submit_order_type)).setText(CodeUtils.getIdString(R.string.string_jiuCanWay));
                }

            } else {//未扫码
                isNeedQRCode = true;
                get(R.id.layout_submit_order_seatNo).setVisibility(View.VISIBLE);
                get(R.id.layout_submit_order_seatYes).setVisibility(View.GONE);
                //外带
                ((TextView) get(R.id.tv_submit_order_seat)).setText(CodeUtils.getIdString(R.string.string_waiDai));
                ((TextView) get(R.id.tv_submit_order_type)).setText(CodeUtils.getIdString(R.string.string_jiuCanWay));

            }
        }
    }

    /**
     * 有未支付订单时使用的设置就餐方式
     *
     * @param seat
     */
    public void setSeatData(String seat) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_submitOrder_canteen) {//食堂
            isNeedQRCode = true;
        } else if (checkedId == R.id.rb_submitOrder_takeout) {//外带
            isNeedQRCode = false;
        }
    }

    /**
     * 获取就餐方式  0=堂食；1=外带
     * 默认值是   0
     *
     * @return
     */
    public int getEatWay() {
        if (!TextUtils.isEmpty(SpUtils.getSeatId())) {
            return 0;
        }
        return 1;
    }

    /**
     * @return
     */
    public int getUserNub() {
        if (SpUtils.getUserNub() != 0) {
            return SpUtils.getUserNub();
        }
        return 0;
    }

    /**
     * 获取备注
     *
     * @return
     */
    public String getRemark() {
        return ((EditText) get(R.id.et_submitOrder_remark)).getText().toString();
    }
}
